define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Comment_seller, Auth, GoDialog, $timeout, GoPage, $ionicSlideBoxDelegate) {
		
		$scope.datasArr = new Array();//存放一览数据
		$scope.nPageArr = [1,1,1];//页数
		$scope.input = new Object();
		$scope.input.answer = "";
		$scope.goods_point = "";//默认加载全部
		$scope.selectIndex = 0;
		
		$scope.$on('$ionicView.afterEnter', function() {
			/*
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			var all = window.innerHeight;
			$scope.myObj = {
				"height": all - 140 - 44 - lan + "px"
			}
			*/
			GoPage.init_top($scope,44,0,130);
			$scope.init_data();
		}, false);
		
		
		//切换页面
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.selectIndex);
			if($scope.selectIndex==1){
				$scope.goods_point = 3;//中评
			}else if($scope.selectIndex==2){
				$scope.goods_point = 1;//差评
			}else{
				$scope.goods_point = "";//全部
			}
			if(!$scope.datasArr[$scope.selectIndex]){
				$scope.init_data();
			}
		};
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};

		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};

		
		//初始化数据  3中评    1差评    其他为全部 
		$scope.init_data = function() {
			$scope.firstHide = false;//避免闪现“没有数据”
			Comment_seller.discuss_list($scope.goods_point, 1, function(data) {
				$scope.datasArr[$scope.selectIndex] = data;
				if($scope.selectIndex==0&&data){//不要过多的执行，控制星星的样式
					$scope.goodsStarInt = parseInt(data.total_score[0].point); //取整
					$scope.serverStarInt = parseInt(data.total_score[1].point);
					$scope.speedStarInt = parseInt(data.total_score[2].point); 
				}
				$scope.firstHide = true;
			})
		}

		//查看回复
		$scope.showAnswer = function(isAddAnswer,index){
			var data = $scope.datasArr[$scope.selectIndex].discuss[index];
			if(isAddAnswer){//如果是追评里的回复
				if(data.showAddAnswer){
					data.showAddAnswer = false;
				}else{
					data.showAddAnswer = true;
				}
			}else{
				if(data.showAnswer){
					data.showAnswer = false;
				}else{
					data.showAnswer = true;
				}
			}
		}

		//显示回复框的输入框
		$scope.showReplyInput = function(isAddReply,index){
			console.log(index)
			$scope.input.answer = "";
			var data = $scope.datasArr[$scope.selectIndex].discuss[index];
			if(isAddReply){//如果是追评里的回复
				if(data.showAddReplyInput){
					data.showAddReplyInput = false;
				}else{
					data.showAddReplyInput = true;
				}
			}else{
				if(data.showReplyInput){
					data.showReplyInput = false;
				}else{
					data.showReplyInput = true;
				}
			}
		}

		//初评回复
		$scope.answer = function(discuss_id) {
			if(!$scope.input.answer){
				GoDialog.toast("请输入回复的内容");
				return;
			}
			Comment_seller.answer(discuss_id, $scope.input.answer, function(data) {
				console.log(data)
				if(data.success) {
					GoDialog.toast("回复成功！");
					$scope.init_data();
				} else {
					GoDialog.alert("提示",data.msg);
				}
			});
		}

		/*===================下拉刷新 =============*/
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Comment_seller.discuss_list($scope.goods_point, 1, function(data) {
				$scope.datasArr[$scope.selectIndex] = data;
				$scope.nPageArr[$scope.selectIndex] = 1;
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Comment_seller.discuss_list($scope.goods_point, $scope.nPageArr[$scope.selectIndex] + 1, function(data) {
//				console.log(data.discuss)
					if(data != null) {
						if($scope.datasArr[$scope.selectIndex] != undefined) {
							$scope.datasArr[$scope.selectIndex].discuss = $scope.datasArr[$scope.selectIndex].discuss.concat(data.discuss);
							$scope.nPageArr[$scope.selectIndex] += 1;
//							console.log($scope.nPageArr)
						} else {
							$scope.datasArr[$scope.selectIndex] = data;
						}
					}
				},function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		}
		/*==================下拉刷新 =============*/

		//页面跳转
		$scope.onshow_detail = function(goods_id) {
			GoPage.go_states("productdetail", {
				"goods_id": goods_id,
				"type": "goods"
			});
		};

		//图片放大
		$scope.showBigImg = function(imgUrl) {
			GoDialog.showBigImg(imgUrl);
		}


	}

	controller.$inject = ['$scope', 'Comment_seller', 'Auth', 'GoDialog', '$timeout', 'GoPage', '$ionicSlideBoxDelegate'];

	return app.registerController("comment_sellerctrl", controller);
});